from pathlib import Path
from datetime import timedelta
# 项目目录
BASE_DIR = Path(__file__).resolve().parent.parent
# 用户上传文件默认保存目录
MEDIA_ROOT = 'upload_files/'
# 服务器访问文件保存的路径
MEDIA_URL = '/upload_files/'

# 指定自定义用户类
AUTH_USER_MODEL = 'Admins.Administrators'

# 项目密钥，项目创建时随机生成。应为唯一的、不可预测的值，用于sessions、messages、密码生成和验证，加密规则等
SECRET_KEY = "django-insecure-x6zhm6&30%5+kc-s^$+_*)jc=fa%nq$-3n%dv%2n==q-*@v&@q"

# 调试模式
DEBUG = True

# 可以访问的域名/主机，当debug为false的时候需要设置参数
ALLOWED_HOSTS = []

# 允许所有用户跨域请求
CORS_ORIGIN_ALLOW_ALL = True

# 启用的所有APP的列表
INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    'apk',
    "Admins",
    # DRF
    'rest_framework',
    # jwt登录认证
    'rest_framework_simplejwt',
    'corsheaders',
]
CORS_ORIGIN_WHITELIST = ("")
# 允许所有的用户跨域请求
# CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True

# 中间件列表
MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "corsheaders.middleware.CorsMiddleware",
    # token校验需要注释掉
    # "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
    # 支持跨域请求的中间件
    "corsheaders.middleware.CorsMiddleware",

]

# 项目所有URL开始的位置（根路由）
ROOT_URLCONF = "apk_parse_model.urls"


# HTML等静态文件的目录
TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [BASE_DIR / 'templates']
        ,
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

# runserver的启动参数
WSGI_APPLICATION = "apk_parse_model.wsgi.application"


# 数据库的设置
# 本机root用户下的appdescams库
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.mysql",
        "NAME": "APPDeScams",
        "USER": "root",
        "PASSWORD": "20050213w",
        "HOST": "127.0.0.1",
        "PORT": "3306",
        'OPTIONS': {
            'charset': 'utf8mb4',
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES';",
            'collation': 'utf8mb4_unicode_ci',
        },
    }
}

DEFAULT_CHARSET = 'utf-8'

# 检查用户密码强度的验证程序列表
AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
    },
]

# 使用中国语言
LANGUAGE_CODE = "zh-hans"
# 使用中国时间
TIME_ZONE = "Asia/shanghai"
# Django的翻译系统
USE_I18N = True
# 时区感知
USE_TZ = True

# 静态请求的路径
STATIC_URL = "static/"
# 用于没有主键的模型的默认字段
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

# 允许所有的用户跨域请求
CORS_ORIGIN_ALLOW_ALL = True

CORS_ALLOWE_ALL_ORIGINS = True
# DRF的配置
REST_FRAMEWORK = {
    # 配置登录鉴权方式
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    )
}

# jwt相关配置
# TOKEN相关配置
SIMPLE_JWT = {
    'ACCESS_TOKEN_LIFETIME': timedelta(minutes=60),
    'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
    'ROTATE_REFRESH_TOKENS': False,
    'BLACKLIST_AFTER_ROTATION': True,
    'ALGORITHM': 'HS256',
    'SIGNING_KEY': SECRET_KEY,
    'VERIFYING_KEY': None,
    'AUDIENCE': None,
    'ISSUER': None,
    'AUTH_HEADER_TYPES': ('Bearer',),
    'AUTH_HEADER_NAME': 'HTTP_AUTHORIZATION',
    'USER_ID_FIELD': 'id',
    'USER_ID_CLAIM': 'user_id',
}
# 使用用户自定义的认证模块
AUTHENTICATION_BACKENDS = [
    'Admins.authenticate.MyBackend'
]